var bigforms_items={};

function bigforms_add_push(id,obj)
{
	if (top === self) {
		bigforms_items[id]=obj;
	} else {
		top.bigforms_add_push(id,obj);
	}
}

function bigforms_push(id,val)
{
	if (top === self) {
		bigforms_items[id]._setOption("value",val);
		delete bigforms_items[id];
		$("#"+id).remove();
	} else {
		top.bigforms_push(id,val);
	}
}

function bigformsfk_set_iframe_url(dialogid,url)
{
	$("#"+dialogid+" iframe").attr("src",url);
}

function call_bigforms_push(obj)
{
	bigforms_push(obj._pushas,obj.value);
}



(function( $, undefined ) {

$.widget("ui.ajajform_bigformsfk",$.ui.ajajform_widget, {
	options: {
		// \type cssclass
		cssclass: "afw-bigformsfk",
		value: {},
		multiselect: false,
		label: "",
		dialog: {width: 1000,height: 600},
		width: null,
		config: {},
		not_null: false,
		empty: "",
		htmltemplate_single: '<div class="af-label"></div><div class="af-value"><div class="af-item"></div><div class="af-error"></div></div><div class="af-button af-button-edit"></div><div style="clear: both;"></div>',
		htmltemplate_multiselect: '<div class="af-label"></div><div class="af-value"><div class="af-items"></div><div class="af-buttons-add"><div class="af-button af-button-add"></div><div style="clear: both;"></div><div class="af-error"></div></div></div><div style="clear: both;"></div>',
		readonly: false
	},
	i18n: {
		ru: {create_button:"Создать",edit_button:"Сохранить",err_empty:"Поле должно быть заполнено"},
		en: {create_button:"Create",edit_button:"Save",err_empty:"Field must be filled"}
	},
	/**
	 * Creates everything, called on construction
	 */
	_create: function() {
		this.element.addClass(this.options.cssclass);
		this.element.append(this.options.multiselect?this.options.htmltemplate_multiselect:this.options.htmltemplate_single);
		this.refreshLabel();
		this.refreshWidth();
		var t=this;	
		$(".af-button",this.element).bind("click",function() {t.newEditClicked();});
		this.refreshValue();
		this.refreshReadonly();
	},
	/**
	 *
	 */
	refreshReadonly: function() {
		if (this.options.readonly) this.element.addClass("afw-bigformsfk-read-only"); else  this.element.removeClass("afw-bigformsfk-read-only");
	},
	/**
	 *
	 */
	newEditClicked: function() {
		if (this.options.readonly) return;
		var val;
		if (!this.options.multiselect) val=this.options.value;
		this.popupOpen(val);
	},
	/**
	 * 
	 */
	editClicked: function(i) {
		if (this.options.readonly) return;
		var v;
		if (this.options.multiselect) v=this.options.values[i]; else v=this.options.value;
		this.popupOpen(v,i);
	},
	/**
	 * Creates and opens a popup dialog to choose existing or enter new
	 */
	popupOpen: function(val,i) {
		var t=this;
		var dialogoptions={
			width: t.options.dialog.width,
			height: t.options.dialog.height
		};
		top.html_dialog(dialogoptions,"<iframe border='0' frameborder='0' src='' width='100%' height='100%'></iframe>");
		t.dialogid=dialogoptions.id;
		var params="/bigforms-popup/?formname="+this.options.config.form+"&_pushas="+dialogoptions.id+(val?"&val="+val.id:"");
		bigforms_add_push(dialogoptions.id,this);
		for (var k in this.options.config) {
			if (k=="form" || k=="filters") continue;
			params+="&_"+k+"="+this.options.config[k];
		}
		if (this.options.config.filters) {
			for (var k in this.options.config.filters) {
				params+="&_filter_"+k+"="+this.options.config.filters[k];
			}
		}
//		console.log(val);
		console.log(params);
		top.bigformsfk_set_iframe_url(t.dialogid,params);
	},
	/**
	 */
	popupNewSelected: function(r,r2) {
		this.selected(r);
	},
	/**
	 * Called on selection of available or on submit when entering new
	 *
	 * @param {integer} id - id or null. id if entry exists
	 * @param {string} name - name to display
	 * @param {Object} object - object or null. Object if non-exisistent entry added, null if existent
	 */
	selected: function(object) {
//		if (object) id=object;
		if (!this.options.multiselect) {
			this.options.value=object;
		} else {
			console.log(object);
			if ("i" in object) {
				this.options.values.splice(object.i,1,object);
			} else {
				this.options.values.push(object);
			}
		}
		this.refreshValue();
		$("#"+this.dialogid).remove();
	},
	/**
	 * Called on removal
	 */
	deselected: function(pos) {
		if (this.options.multiselect) {
			this.options.values.splice(pos,1);
		} else {
			this.options.value={};
		}
		this.refreshValue();
	},
	/**
	 *
	 */
	refreshWidth: function() {
		if (this.options.multiselect) {
			$(".af-value",this.element).width(this.options.width);
			$(".af-value2",this.element).width(this.options.width);
		} else {
			$(".af-value",this.element).width(this.options.width-19);
			$(".af-value2",this.element).width(this.options.width-19);
		}
	},
	/**
	 *
	 */
	refreshLabel: function() {
		$(".af-label",this.element).text(this.options.label);
	},
	/**
	 *
	 */
	genName: function(val) {
		if (!this.options.form_result_name) return val.name;
		var f=this.options.form_result_name;
		if (typeof f == "function") return f(val);
		return val[f];
	},
	/**
	 *
	 */
	refreshValue: function() {
		var t=this;
		if (this.options.multiselect) {
			var div=$(".af-value .af-items",this.element);
			var v=this.options.values;
			div.empty();
			for (var i=0;i<v.length;i++) {
				var div2=$("<div class='af-item'><div class='af-content'></div><div class='af-button-edit'></div><div class='af-button-delete'></div><div class='cl'></div></div>").appendTo(div);
				$(".af-content",div2).html(v[i].html).width(this.options.width-19*2);
				$(".af-button-delete",div2).bind("click",{i:i},function(e) {t.deselected(e.data.i)});
				$(".af-button-edit",div2).bind("click",{i:i},function(e) {t.editClicked(e.data.i)});
			}
			if (v.length==0) div.text(this.options.empty);
		} else {
			var div=$(".af-value .af-item",this.element);
			div.html(this.options.value.html);
		}
	},
	postvalue: function(r) {
		if (this.options.multiselect) {
			var arr=[];
			var v=this.options.values;
			for (var i=0;i<v.length;i++) arr.push(v[i].id);
			r[this.options.name]=arr.join(",");
		} else {
			r[this.options.name]=this.options.value.id;
		}
	},
	validate: function() {
		var vd=this.options.validate;
		var r;
/*		if (vd) r=vd(this.options.multiselect?this.options.values:this.options.value);
		if (!r) {
			if (this.options.not_null) {
				if (this.options.multiselect) {
					if (!this.options.values ||this.options.values.length==0) r=this.gettext("err_empty");
				} else {
					var i;
					if (this.options.value) for (var k in this.options.value) i=1;
					if (!i) r=this.gettext("err_empty");
				}
			}
		}
		this.refreshError(r);*/
		return r?true:false;
	}
});

})( jQuery );

